from sqlalchemy.schema import *
from sqlalchemy.types import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from pythonblog.model import meta
from pythonblog.lib.helpers import now


class UserProfile(meta.Base):
    __tablename__ = 'user_profile'

    id = Column(Integer, primary_key=True)
    userName = Column(String(30), nullable=False, unique=True)
    createDate = Column(DateTime, nullable=False)
    updateDate = Column(DateTime)
    loginDate = Column(DateTime)
    
    realName = Column(Unicode(50))
    gender = Column(Integer)
    email = Column(String(50), unique=True)
    city = Column(Unicode(30))
    vocation = Column(Unicode(30))
    industry = Column(Unicode(30))
    userLogo = Column(String(50))
    viewTimes = Column(Integer, nullable=False)
    
    posts = relation('Post', lazy='dynamic', order_by='desc(Post.createDate)')
    comments = relation('Comment', lazy='dynamic', 
                        order_by='desc(Comment.createDate)')

    def __init__(self):
        self.createDate = now()
        self.viewTimes = 0

    def __repr__(self):
        return '<UserProfile "%s">' % self.userName
    
